package cn.dinkevin.xui.widget.scroll;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ScrollView;

import cn.dinkevin.xui.util.LogUtil;

/**
 * 引用第三方界面下拉后，首个 View 放大效果控件 </br>
 * 项目地址：https://github.com/whichname/HeadZoomScrollView
 * Created by whichname on 2017/1/15 0015.
 */

public class HeadZoomScrollView extends ScrollView {

    public HeadZoomScrollView(Context context) {
        super(context);
    }

    public HeadZoomScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public HeadZoomScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    // 用于记录下拉位置
    private float y = 0f;
    // zoomView 原本的宽高
    private int zoomViewWidth = 0;
    private int zoomViewHeight = 0;

    // 是否正在放大
    private boolean scaling = false;

    // 放大的view，默认为第一个子view
    private View zoomView;
    public void setZoomView(View zoomView) {
        this.zoomView = zoomView;
    }

    // 滑动放大系数，系数越大，滑动时放大程度越大
    private float scaleRatio = 0.4f;
    public void setScaleRatio(float scaleRatio) {
        this.scaleRatio = scaleRatio;
    }

    // 最大的放大倍数
    private float scaleTimes = 2f;
    public void setScaleTimes(int scaleTimes) {
        this.scaleTimes = scaleTimes;
    }

    // 回弹时间系数，系数越小，回弹越快
    private float replyRatio = 0.5f;
    public void setReplyRatio(float replyRatio) {
        this.replyRatio = replyRatio;
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        // 不可过度滚动，否则上移后下拉会出现部分空白的情况
        setOverScrollMode(OVER_SCROLL_NEVER);
        // 获得默认第一个view
        if (getChildAt(0) != null && getChildAt(0) instanceof ViewGroup && zoomView == null) {
            ViewGroup vg = (ViewGroup) getChildAt(0);
            if (vg.getChildCount() > 0) {
                zoomView = vg.getChildAt(0);
            }
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        if (zoomViewWidth <= 0 || zoomViewHeight <=0) {
            zoomViewWidth = zoomView.getMeasuredWidth();
            zoomViewHeight = zoomView.getMeasuredHeight();
        }
        if (zoomView == null || zoomViewWidth <= 0 || zoomViewHeight <= 0) {
            return super.onTouchEvent(ev);
        }
        switch (ev.getAction()) {
            case MotionEvent.ACTION_MOVE:
                if (!scaling) {
                    if (getScrollY() == 0) {
                        y = ev.getY();//滑动到顶部时，记录位置
                    } else {
                        break;
                    }
                }
                int distance = (int) ((ev.getY() - y)* scaleRatio);
                if (distance < 0) break;//若往下滑动
                scaling = true;
                setZoom(distance);
                return true;
            case MotionEvent.ACTION_UP:
                scaling = false;
                replyView();
                break;
        }
        return super.onTouchEvent(ev);
    }

    /**
     * 放大 view
     */
    private void setZoom(float s) {
        float scaleTimes = (float) ((zoomViewWidth + s)/( zoomViewWidth * 1.0));
        //  如超过最大放大倍数，直接返回
        if (scaleTimes > this.scaleTimes) return;

        ViewGroup.LayoutParams layoutParams = zoomView.getLayoutParams();
        layoutParams.width = (int) (zoomViewWidth + s);
        layoutParams.height = (int)(zoomViewHeight * ((zoomViewWidth + s) / zoomViewWidth));
        //  设置控件水平居中
        ((MarginLayoutParams) layoutParams).setMargins(-(layoutParams.width - zoomViewWidth) / 4, 0, 0, 0);
        zoomView.setLayoutParams(layoutParams);

        LogUtil.d(s,"width:",layoutParams.width,"height:",layoutParams.height,-(layoutParams.width - zoomViewWidth) / 2);
    }

    /**
     * 回弹
     */
    private void replyView() {
        final float distance = zoomView.getMeasuredWidth() - zoomViewWidth;
        // 设置动画
        ValueAnimator anim = ObjectAnimator.ofFloat(distance, 0.0F).setDuration((long) (distance * replyRatio));
        anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                setZoom((Float) animation.getAnimatedValue());
            }
        });
        anim.start();
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldL, int oldT) {
        super.onScrollChanged(l, t, oldL, oldT);
        if (onScrollListener != null) onScrollListener.onScroll(l,t,oldL,oldT);
    }

    private OnScrollListener onScrollListener;
    /**
     * 设置滑动监听
     * @param onScrollListener
     */
    public void setOnScrollListener(OnScrollListener onScrollListener) {
        this.onScrollListener = onScrollListener;
    }

    /**
     * 声明滑动监听
     */
    public  interface OnScrollListener{
        void onScroll(int scrollX, int scrollY, int oldScrollX, int oldScrollY);
    }
}